package com.test.daily.leetcode.y2022.m06.day0613.v03;

import java.util.Arrays;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/6/13 下午 11:02
 * @version: 1.0
 */
public class Soluton {
    public int heightChecker(int[] heights) {
        int[] origin = new int[heights.length];
        for (int i = 0; i < heights.length; i++) {
            origin[i] = heights[i];
        }
        Arrays.sort(heights);
        int ans = 0;
        for (int i = 0; i < heights.length; i++) {
            if (heights[i] != origin[i]) {
                ans++;
            }
        }
        return ans;
    }

    public int heightChecker2(int[] heights) {
       int max = Integer.MIN_VALUE;
        for (int i = 0; i < heights.length; i++) {
            max = Math.max(max, heights[i]);
        }
        int[] counts = new int[max+1];
        for(int ele : heights){
            counts[ele]++;
        }
        int ans = 0;
        int inx = 0;
        for(int i = 0; i <= max; i++){
            for(int j = 0; j < counts[i]; j++){
                if(heights[inx] != i){
                    ++ans;
                }
                inx++;
            }
        }
        return ans;
    }

}